package com.example.springvue.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.springvue.common.annotation.LogAnnotation;
import com.example.springvue.common.annotation.PassToken;
import com.example.springvue.common.handler.AdminUserHeaderHodler;
import com.example.springvue.common.handler.NotExistException;
import com.example.springvue.common.jwt.JwtUtils;
import com.example.springvue.common.jwt.Md5Utils;
import com.example.springvue.common.returnthrow.Result;
import com.example.springvue.model.AdminUser;
import com.example.springvue.model.AuthGroup;
import com.example.springvue.service.AdminUserService;
import com.example.springvue.service.AuthGroupService;
import com.example.springvue.service.AuthRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@Controller
public class MenuController {


    @Autowired
    AdminUserService adminUserService;
    @Autowired
    AuthRuleService authRuleService;
    @Autowired
    AuthGroupService authGroupService;


    @RequestMapping("/admin/getMenu")
    @ResponseBody
    @LogAnnotation(module = "用户模块",operator = "获取登录用户信息的菜单")
    public Result<List<Map< String, Object>>> getMenu(){
        AdminUser adminUser = AdminUserHeaderHodler.get();
        AuthGroup authGroup = authGroupService.getAuthGroupInfoId(adminUser.getGroupId());
        List<Map< String, Object >> authRule = authRuleService.getAuthRuleList(null);
        List<Map< String, Object >> wdadaw = new ArrayList<>();
        for (int i = 0; i < authRule.size(); i++) {
            Map< String, Object > info = authRule.get(i);
            String id = String.valueOf((Long) info.get("id"));
            List<Map< String, Object >> children = authRuleService.getAuthRuleListTwo(id);
            info.put("children",children);
            System.out.println(children);
            wdadaw.add(info);
        }
        return Result.success("请求成功",wdadaw);
    }


    /**
     * 菜单管理
     * 权限管理的菜单列表
     *
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/admin/menu/index")
    public Result<List<Map< String, Object >>> getMenu(@RequestBody Map<String, String> Body) throws NotExistException {
        try{
            AdminUser adminUser = AdminUserHeaderHodler.get();
            AuthGroup authGroup = authGroupService.getAuthGroupInfoId(adminUser.getGroupId());
            List<Map< String, Object >> authRule = null;
            if (Body.get("id")==null||Body.get("id")==""){
                authRule = authRuleService.getAuthRule();
            }else {
                 authRule = authRuleService.getAuthRuleTwo(Body.get("id"));
            }
            List<Map< String, Object >> list = new ArrayList<>();
            for (int i = 0; i < authRule.size(); i++) {
                Map< String, Object > info = authRule.get(i);
                String pid = String.valueOf((Long) info.get("id"));
                List<Map< String, Object >> children = authRuleService.getAuthRuleListTwo(pid);
                boolean  hasChildren = true;
                if (children.isEmpty()){
                    hasChildren = false;
                }
                info.put("hasChildren",hasChildren);
                list.add(info);
            }

            return Result.success("请求成功",list);
        }
        catch (NotExistException e)
        {
            throw new NotExistException(e.getMessage());
        }
    }




    /**
     * 角色管理
     * 级联下拉菜单
     *
     * @return
     */

    @ResponseBody
    @PostMapping(value = "/admin/menu/cascade")
    public Result<List<Map< String, Object >>> getMenuCascade(@RequestBody Map<String, String> Body) throws NotExistException {
        try{
            List<Map< String, Object >>  authRule = authRuleService.getAuthRuleCascade();
            return Result.success("请求成功",authRule);
        }
        catch (NotExistException e)
        {
            throw new NotExistException(e.getMessage());
        }
    }



    /**
     *
     * 保存
     *
     * @return
     */
    @LogAnnotation(module = "菜单模块",operator = "添加和保存")
    @ResponseBody
    @PostMapping(value = "/admin/menu/save")
    public Result<Boolean> getMenuSave(@RequestBody Map<String, Object> Body) throws NotExistException {
        try{
            int id = (int) Body.get("id");
            System.out.println(Body);
            if (id == 0){
               boolean add= authRuleService.authRuleAdd(Body);
               if (add){
                   return Result.success("添加成功",null);
               }else {
                   return Result.failure("添加失败");
               }
            }else {
                boolean edit= authRuleService.authRuleEdit(Body);
                if (edit){
                    return Result.success("修改成功",null);
                }else {
                    return Result.failure("修改失败");
                }
            }
        }
        catch (NotExistException e)
        {
            throw new NotExistException(e.getMessage());
        }
    }

    /**
     *
     * 删除
     *
     * @return
     */
    @LogAnnotation(module = "菜单模块",operator = "删除菜单")
    @ResponseBody
    @PostMapping(value = "/admin/menu/delete")
    public Result<Boolean> MenuDelete(@RequestBody Map<String, Object> Body) throws NotExistException {
        try{
            int id = (int) Body.get("id");
            List<Map< String, Object >>  authRule = authRuleService.getAuthRuleTwo(String.valueOf(id));
            if (CollectionUtils.isNotEmpty(authRule)) {
                return Result.failure("该菜单有下级，无法删除");
            }
            boolean del = authRuleService.authRuleDelete(String.valueOf(id));
            if (del){
                return Result.success("删除成功",null);
            }else {
                return Result.failure("删除失败");
            }

        }
        catch (NotExistException e)
        {
            throw new NotExistException(e.getMessage());
        }
    }


}
